<template>
	<view>
		<view class="content">
			<view class="set-item" @click="setAssist">
				<view class="set-item-label"><text>打招呼语设置</text></view>
				<view class="set-item-value">
					<i v-show="data.greetsData.greetings_disable === '1'" class="examine-type">被禁用</i>
					<icon class="iconfont icon">&#xe7d3;</icon>
				</view>
			</view>
			<view class="set-item">
				<view class="set-item-label set-label">
					<text>{{ data.greetsData.greetings ? data.greetsData.greetings : '选择或自定义您的打招呼用语' }}</text>
				</view>
			</view>
		</view>
		<view class="content">
			<view class="set-item">
				<view class="set-item-label"><text>自动打招呼</text></view>
				<view class="set-item-value">
					<i v-show="data.greetsData.isgreetings === '1'" class="examine-type">被禁用</i>
					<view class="mantle" @click="changeIsAutomatic"></view>
					<switch :checked="data.greetsData.autogreetings === '1'" :disabled="data.greetsData.isgreetings === '1'" style="transform:scale(0.8)" />
				</view>
			</view>
			<view class="set-item">
				<view class="set-item-label set-label"><text>功能开启后，系统将自动给来源于您上传的文档的客户打招呼</text></view>
			</view>
		</view>
	</view>
	<assist-set ref="assistSetRef" @assistSetCallback="assistSetCallback"></assist-set>
	<server-time ref="serverTimeRef" @timeLayerCallback="timeLayerCallback"></server-time>
</template>

<script setup>
import { ref, reactive, defineComponent } from 'vue';
import { greetings } from '@/subpages/common/api/set.js';
import { showModal, showCopy } from '@/common/hooks/useShow.js';
import config from '@/uni.config.js';
import assistSet from '../components/assist-set.vue';
import serverTime from '../components/server-time.vue';
import { startAutogreetings, isBindSubscribe, closeAutogreetings } from '@/subpages/common/api/set.js';
const assistSetRef = ref(null);
const serverTimeRef = ref(null);
const data = reactive({
	greetsData: {}
});
defineComponent({
	assistSet,
	serverTime
});

async function init() {
	const res = await greetings();
	data.greetsData = res.data;
}
init();
//设置打招呼语
function setAssist() {
	if (data.greetsData.greetings_disable === '1') {
		showModal({
			title: '提示',
			content: `禁用原因：${data.greetsData.greetings_reason}`,
			confirmText: '我知道了',
			showCancel: false
		});
	} else {
		assistSetRef.value.open();
	}
}
//选择打招呼语弹窗回调传值
function assistSetCallback(callbackData) {
	data.greetsData.greetings = callbackData;
}
//是否自动开启打招呼
function changeIsAutomatic() {
	if (data.greetsData.isgreetings === '1') {
		showModal({
			title: '提示',
			content: `禁用原因：${data.greetsData.isgreetings_reason}`,
			confirmText: '我知道了',
			showCancel: false
		});
	} else {
		isOpenGreetingsRequest();
	}
}
//判断是否可以开通自动打招呼
function isOpenGreetingsRequest() {
	if (data.greetsData.isgreetings === 0) {
		showModal({
			title: '提示',
			content: `您不满足开通自动打招呼的条件，无法开通本功能有疑问请联系工作人员：${config.customer.admin}`,
			confirmText: '我知道了',
			confirm() {
				// showCopy({
				// 	data: config.customer.admin,
				// 	type: 1,
				// 	icon: 'none',
				// 	title: '已复制'
				// });
			}
		});
	} else {
		if (data.greetsData.autogreetings === '1') {
			data.greetsData.autogreetings = '0';
			closeAutogreetingsRequest();
		} else {
			//打开时间设置弹窗
			serverTimeRef.value.open(data.greetsData.worktimestart, data.greetsData.worktimeend, '1');
		}
	}
}
function timeLayerCallback(callbackData) {
	data.greetsData.autogreetings = callbackData;
	isBindSubscribeRequest(callbackData);
}
// 判断是否关注公众号
async function isBindSubscribeRequest(workTime) {
	const res = await isBindSubscribe();
	if (res.code == -200) {
		showModal({
			title: '提示',
			content: '开启本功能需关注公众号\n（用于即时接收、回复客户消息）',
			confirmText: '去关注',
			confirm() {
				uni.navigateTo({
					url: `/subpages/web/web?url=${encodeURIComponent(config.article.public)}`
				});
			}
		});
	} else {
		if (res.code == 200) {
			startAutogreetingsRequest(workTime);
		}
	}
}
//关闭自动打招呼
async function closeAutogreetingsRequest() {
	const res = await closeAutogreetings();
}
//辅助功能自动打招呼提交
async function startAutogreetingsRequest(workTime) {
	const res = await startAutogreetings(workTime);
	data.greetsData.autogreetings = '1';
	serverTimeRef.value.close();
}
</script>
<style lang="scss">
@import '@/common/css/base.scss';
page {
	background-color: $bgColor;
}
</style>
<style lang="scss" scoped>
@import '@/subpages/common/css/set.scss';
.content {
	margin: 28rpx;
	border-radius: 16rpx;
	overflow: hidden;
	background-color: #fff;
	.set-label {
		color: #aaa;
		font-size: 24rpx;
	}
	.set-item-value {
		position: relative;
	}
	.mantle {
		position: absolute;
		right: 0;
		top: -19rpx;
		width: 100rpx;
		height: 100rpx;
		z-index: 9;
	}
	.examine-type {
		width: 118rpx;
		height: 44rpx;
		line-height: 44rpx;
		font-size: 22rpx;
		text-align: center;
		color: #fff;
		border-radius: 6rpx;
		background: #ff0000;
	}
}
</style>
